package com.voe;

import com.voe.callback.Encryption;
import com.voe.common.Utility;

/**This sub-API supports the following functionalities:<br/>
*<br/>
*- External encryption and decryption.<br/>
*<br/>
*Usage example, omitting error checking:<br/>
*<br/>
*VoiceEngine voe = VoiceEngine.createInterface();<br/>
*VoEEncryption encrypt  = VoEEncryption.createInterface(voe);<br/>
*...<br/>
*encrypt.release();<br/>
*voe.release();<br/>
*/
public class VoEEncryption {

	// [start] apis

	public static VoEEncryption createInstance(VoiceEngine voiceEngine) {

		long p = GetInterface(voiceEngine._p);
		if(p != 0)
			return new VoEEncryption(p);
		return null;
	}

	public void release() {
		if (_p != 0) {
			Release(_p);
		}
	}
	
	/** Installs an Encryption instance and enables external encryption<br/>
	 *  for the selected |channel|.
	 */
	public int registerExternalEncryption(int channel, Encryption encryption) {
		Utility.throwNullPointerException(encryption, "encryption");
		return RegisterExternalEncryption(_p, channel, encryption);
	}

	/** Removes an Encryption instance and disables external encryption<br/>
	 *  for the selected |channel|.
	 */
	public int deRegisterExternalEncryption(int channel) {
		return DeRegisterExternalEncryption(_p, channel);
	}

	protected VoEEncryption(long p) {
		_p = p;
	}

	protected long _p;
	
	// [end]
	
	
	
	

	// [start] jni api

	// Factory for the VoEEncryption sub-API. Increases an internal
	// reference counter if successful. Returns NULL if the API is not
	// supported or if construction fails.
	private static native long GetInterface(long p_voiceEngine);

	// Releases the VoEEncryption sub-API and decreases an internal
	// reference counter. Returns the new reference count. This value should
	// be zero for all sub-API:s before the VoiceEngine object can be safely
	// deleted.
	private static native int Release(long p_VoEEncryption);

	// Installs an Encryption instance and enables external encryption
	// for the selected |channel|.
	private static native int RegisterExternalEncryption(long p_VoEEncryption,
			int channel, Encryption encryption);

	// Removes an Encryption instance and disables external encryption
	// for the selected |channel|.
	private static native int DeRegisterExternalEncryption(
			long p_VoEEncryption, int channel);

	// [end]
}
